package tresp.bthreads;

import java.util.logging.Logger;

import robocode.BattleEndedEvent;
import robocode.BulletHitBulletEvent;
import robocode.BulletHitEvent;
import robocode.BulletMissedEvent;
import robocode.CustomEvent;
import robocode.DeathEvent;
import robocode.HitByBulletEvent;
import robocode.HitRobotEvent;
import robocode.HitWallEvent;
import robocode.MessageEvent;
import robocode.RobotDeathEvent;
import robocode.RoundEndedEvent;
import robocode.ScannedRobotEvent;
import robocode.StatusEvent;
import robocode.WinEvent;
import tresp.coordinator.ICoordinator;
import tresp.utils.BThreadVote;
import tresp.utils.IEventResponder;
import tresp.utils.logging.MyLogger;



/**
 * 
 * <b>BThread</b>
 * 
 * <p>
 * 	This is base class for behavior threads implementation.
 * </p>
 * 
 * 
 * @author <font color="blue">Maxim Kirilov</font>
 *
 */
public abstract class BThread implements Runnable, IEventResponder{
    
    
    private static Logger logger = MyLogger.getLogger( BThread.class );
    
    /**inidicates whethear this b-thread is active or not*/
    protected boolean isActive = true;
    
    /**This b-thread coordinator*/
    protected ICoordinator<BThreadVote> coordinator;
    
    /**b-thread vote weight*/
    protected double weight;
    
    
    public BThread(ICoordinator<BThreadVote> coordinator){
	this.coordinator = coordinator;
    }
    
    
    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight += weight;  
    }

    @Override
    public void run(){
	
	while( isActive ){
	    try{
		vote();
		delay();
	    }catch (Exception e){ logger.severe(getName() + e.toString()); }
	}
    }

    private void delay() {
//	long delay = (3 - coordinator.getLevel()) * 80;
	try{
	    Thread.sleep( 80L );
	}catch(InterruptedException e){}
    }


    public BThreadVote createBThreadVote(BThreadType voteType, double weight, double parameter){
	return new BThreadVote(voteType, weight, parameter);
    }
    
    
    public abstract void vote();
    
    public abstract String getName();

    @Override
    public void onBattleEnded(BattleEndedEvent event){
	stopBThread();
    }

    @Override
    public void onBulletHit(BulletHitEvent event){}

    @Override
    public void onBulletHitBullet(BulletHitBulletEvent event){}

    @Override
    public void onBulletMissed(BulletMissedEvent event){}

    @Override
    public void onDeath(DeathEvent event){
	stopBThread();
    }

    @Override
    public void onHitByBullet(HitByBulletEvent event){}

    @Override
    public void onHitRobot(HitRobotEvent event){}

    @Override
    public void onHitWall(HitWallEvent event){}

    @Override
    public void onRobotDeath(RobotDeathEvent event){}

    @Override
    public void onRoundEnded(RoundEndedEvent event){
	stopBThread();
    }

    @Override
    public void onScannedRobot(ScannedRobotEvent event){}

    @Override
    public void onStatus(StatusEvent e){}

    @Override
    public void onWin(WinEvent event){
	stopBThread();
    }
    
    @Override
    public void onMessageReceived(MessageEvent MessageEvent){}
    
    
    @Override
    public void onCustomEvent(CustomEvent e) {}
    
    private void stopBThread() {
	
	isActive = false;
    }
}
